package com.crm.controller.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.pojo.Contact;
import com.crm.service.client.ContactService;
import com.crm.service.client.impl.ContactServiceImpl;
import com.crm.vo.Result;
import com.crm.vo.WordAndFirstName;

@Controller
public class ContactController {

	@Autowired
	public ContactService contactService;
	
	/**
	 * 转到联系人页面
	 * @return
	 */
	@GetMapping("/to-contact")
	public String toContact() {
		return "client/contact";
	}
	
	/**
	 * 转到添加联系人页面
	 * @return
	 */
	@GetMapping("/to-add-contact")
	public String toAddContact() {
		return "client/add-contact";
	}
	
	/**
	 * 转到编辑联系人页面
	 * @return
	 */
	@GetMapping("/to-edit-contact")
	public String toEditContact() {
		return "client/edit-contact";
	}
	
	/**
	 * 转到编辑联系人页面
	 * @return
	 */
	@GetMapping("/to-view-contact")
	public String toViewContact() {
		return "client/view-contact";
	}
	
	
	/**
	 * 分页查询所有的联系人
	 * @param page
	 * @param rows
	 * @return
	 */
	@PostMapping("/find-all-contacts")
	@ResponseBody
	public Map findAllContacts(int page, int rows){
		Long total = contactService.countContactsNoDel();
		List<Contact> list = contactService.findAllContactsNoDel(page, rows);
		
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	/**
	 * 删除联系人
	 * @param contactid
	 * @return
	 */
	@PostMapping("/del-contact")
	@ResponseBody
	public  Result delContact(Long contactid) {
		return contactService.delContact(contactid);
	}
	
	/**
	 * 获取联系人所有的姓
	 * @return
	 */
	@PostMapping("/contact-first-name")
	@ResponseBody
	public List<WordAndFirstName> getContactFirstName() {
		return contactService.getContactFirstName();
	}

	/**
	 * 通过姓名查找联系人
	 * @param contactName
	 * @return
	 */
	@PostMapping("/find-contact-by-name")
	@ResponseBody
	public List<Contact> findContactByNameNotDel(String contactName) {
		return contactService.findContactByNameNotDel(contactName);
	}
	
	/**
	 * 新建联系人，并为他指明客户
	 * @param contact
	 * @param cid
	 * @return
	 */
	@PostMapping("/add-contact")
	@ResponseBody
	public Result addContact(Contact contact,Long cid) {
		return contactService.addContact(contact,cid);
	}
	
	
	/**
	 * 条件查询联系人
	 * @param contact
	 * @param cid
	 * @return
	 */
	@PostMapping("/query-contact-where")
	@ResponseBody
	public Map queryContactWhere(Contact contact, Long cid, int page, int rows) {
		Long total = contactService.queryCountContactWhere(contact, cid);
		List<Contact> list = contactService.queryContactWhere(contact, cid, page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	@PostMapping("/find-contact-by-contactid")
	@ResponseBody
	public Contact findContactBycontactid(Long contactid) {
		return contactService.findContactBycontactid(contactid);
	}
}
